package com.clz.latte.ec.launcher;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.clz.latte.app.AccountManager;
import com.clz.latte.app.IUserChecker;
import com.clz.latte.delegate.LatteDaleGate;
import com.clz.latte.ec.R;
import com.clz.latte.ui.launcher.ILauncherFinishListener;
import com.clz.latte.ui.launcher.LauncherFinishTag;
import com.clz.latte.ui.launcher.LauncherHolderCreator;
import com.clz.latte.ui.launcher.LauncherScrollTag;
import com.clz.latte.utils.storage.LattePreference;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/12/23.
 */

public class LauncherScrollDelegate extends LatteDaleGate implements OnItemClickListener {
    private ConvenientBanner<Integer> mBanner = null;
    private ArrayList<Integer> INTEGERS = new ArrayList<>();
    private ILauncherFinishListener mILauncherFinishListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ILauncherFinishListener) {
            mILauncherFinishListener = (ILauncherFinishListener) activity;
        }
    }
    @Override
    public Object setLayOut() {
        mBanner = new ConvenientBanner<Integer>(getActivity());
        return mBanner;
    }

    private void initBanner() {
        INTEGERS.add(R.mipmap.launcher_01);
        INTEGERS.add(R.mipmap.launcher_02);
        INTEGERS.add(R.mipmap.launcher_03);
        INTEGERS.add(R.mipmap.launcher_04);
        INTEGERS.add(R.mipmap.launcher_05);
        mBanner.setPages(new LauncherHolderCreator(), INTEGERS)
                .setPageIndicator(new int[]{R.drawable.dot_normal, R.drawable.dot_focus})
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                .setOnItemClickListener(this)
                .setCanLoop(false);
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        initBanner();
    }

    @Override
    public void onItemClick(int position) {
        if (position==INTEGERS.size()-1){
            //用户第一次启动app
            LattePreference.setAppFlag(LauncherScrollTag.HAS_FIRST_ENTER_APP.name(),true);
            //判断用户是否登录
            //判断用户是否登录
            AccountManager.checkAccount(new IUserChecker() {
                /**
                 * 启动结束后，登录过了
                 */
                @Override
                public void onSignIn() {
                    if (null!=mILauncherFinishListener){
                        mILauncherFinishListener.onLauncherFinish(LauncherFinishTag.SIGN_IN);
                    }
                }

                /**
                 * 启动结束后，未注册登录
                 */
                @Override
                public void onNotSignIn() {
                    if (null!=mILauncherFinishListener){
                        mILauncherFinishListener.onLauncherFinish(LauncherFinishTag.NOT_SIGN_IN);
                    }
                }
            });
        }

    }
}
